﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataControl;
using System.Data;
using System.Text;

namespace ATW.MT.BUS
{
    public class bus_C_Material
    {
        DataAccess da = null;
        public bus_C_Material()
        {
            da = new DataAccess();
        }
        #region 方法
        /// <summary>
        /// 材质分页查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public DataTable MaterialPagerQuery(string name, int page, int pagesize)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@"select top 100 percent m.MID,
					   m.Material_code code_d,
					   isnull(m.Material_code_z,'无') code_z,
					   isnull(mm.num,'0') num,
                       Material_rate rate,
                       create_time 
					   from C_Material m left join 
	            (select COUNT(*) num,Mid from C_Material_MultiLan group by MID) mm
                   on m.MID=mm.MID");
            if (name != "")
            {
                sql.Append(" where m.Material_code like '%" + name + "%' or m.Material_code_z like '%" + name + "%'");
            }
            return da.PageSize(sql.ToString(), page, pagesize, "MID", "create_time desc");
        }
        /// <summary>
        /// 材质查询
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="pid"></param>
        /// <returns></returns>
        public DataTable MaterialQuery(string pid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select CM.* from T_Production_Material M 
                                left join C_Material CM on CM.MID=M.MID where ProductionID='{0}' and 
                                type='4' order by CM.Material_code desc", pid);
            return da.ExecuteDataTable(sql.ToString());
        }
        /// <summary>
        /// 新增材质
        /// </summary>
        /// <param name="name">锻件编号</param>
        /// <param name="name_z">铸件代号</param>
        /// <returns></returns>
        public int MaterialAdd(string name, string name_z)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into C_Material (Material_code,Material_code_z)values('{0}','{1}')", name, name_z);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 新增材质
        /// </summary>
        /// <param name="name">铸件编号</param>
        /// <param name="name_z">锻件编号</param>
        /// <param name="rate">价格系数</param>
        /// <returns></returns>
        public int MaterialAdd(string name, string name_z, string rate)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into C_Material (Material_code,Material_code_z,Material_rate)values('{0}','{1}','{2}')", name, name_z, rate);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 修改材质
        /// </summary>
        /// <param name="name">锻件代号</param>
        /// <param name="name_z">铸件代号</param>
        /// <param name="mid">材质编号</param>
        /// <returns></returns>
        public int MaterialEdit(string name, string name_z, string mid)
        {
            string strsql = "update C_material set Material_code='" + name + "'";
            if (!string.IsNullOrEmpty(name_z))
            {
                strsql += ",Material_code_z='" + name_z + "'";
            }
            strsql += " where MID ='" + mid + "'";
            return da.ExcuteNoQuery(strsql);
        }
        /// <summary>
        /// 修改材质
        /// </summary>
        /// <param name="name"></param>
        /// <param name="name_z"></param>
        /// <param name="rate"></param>
        /// <param name="mid"></param>
        /// <returns></returns>
        public int MaterialEdit(string name, string name_z, string rate, string mid)
        {
            string sql = "update C_material set material_code='" + name + "',Material_code_z='" + name_z + "',[Material_rate]='" + rate + "' where [MID]='" + mid + "'";
            return da.ExcuteNoQuery(sql);
        }
        /// <summary>
        /// 材质是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool IsMaterialExist(string name)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from C_Material where [Material_code]='{0}'", name);
            DataTable dt = da.ExecuteDataTable(sql.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 材质描述查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable DescQuery(string id)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select mm.[Desc],mm.MID id,mm.create_time,l.LanName,l.LanId from C_Material_MultiLan mm 
	                                        left join D_Language l on mm.LanId=l.LanId where mm.mid='" + id + "'");
            return da.ExecuteDataTable(sql.ToString());
        }
        /// <summary>
        /// 新增材质描述
        /// </summary>
        /// <param name="mid"></param>
        /// <param name="lanid">语言id</param>
        /// <param name="name"></param>
        /// <returns></returns>
        public int MaterialAddDesc(string mid, string lanid, string desc)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into C_Material_MultiLan (mid,lanid,[desc])values('{0}','{1}','{2}')", mid, lanid, desc);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 修改材质描述
        /// </summary>
        /// <param name="mid"></param>
        /// <param name="lanid"></param>
        /// <param name="desc"></param>
        /// <returns></returns>
        public int MaterialEditDesc(string mid, string lanid, string desc)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@" update C_Material_multiLan set [desc]='{0}' where mid='{1}' and lanid='{2}'", desc, mid, lanid);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 检查描述是否存在
        /// </summary>
        /// <param name="mid"></param>
        /// <param name="lanid"></param>
        /// <returns></returns>
        public bool isDescExist(string mid, string lanid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from  C_Material_multiLan where mid='{0}' and lanid='{1}'", mid, lanid);
            DataTable dt = da.ExecuteDataTable(sql.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获取语言列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetLan()
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from D_Language");
            return da.ExecuteDataTable(sql.ToString());
        }
        #endregion
    }
}